const Controller = require('./baseController');



class RoomController extends Controller {
  async create() {
    let serialNumber = 1;
    let lastRoom = await this.ctx.model.Room.find({}).sort('-insert_time').limit(1)
    if (lastRoom && lastRoom.length && lastRoom[0].serialNumber) serialNumber = lastRoom[0].serialNumber + 1
    try {
      let roomInfo = await this.ctx.model.Room.create({
        serialNumber: serialNumber,
        player: [this.ctx.state.user.userInfo['_id']],
        owner: this.ctx.state.user.userInfo['_id'],
        insert_time: new Date(),
        gameInfo: {
          playerInfo: [{ //玩家状态
            name: this.ctx.state.user.userInfo.username ,
            id: this.ctx.state.user.userInfo['_id'],
            headImage: this.ctx.state.user.userInfo.headImage,
            score: 1000, //!默认初始1000分
          }]
        }
      })
      return this.success(roomInfo)
    } catch (e) {
      return this.error(e.message)
    }
  }

}

module.exports = RoomController;